package com.kjtpay.design_pattern.command;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.kjtpay.design_pattern.R;
import com.kjtpay.design_pattern.adapter.AdapterActivity;

public class CommandActivity extends AppCompatActivity {

    public static void launch(Context context) {
        if (null != context) {
            Intent intent = new Intent();
            intent.setClass(context, CommandActivity.class);
            if (!(context instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        }
    }

    CheckBox muttonCheckBox;
    CheckBox chickenCheckBox;
    CheckBox newCheckBox;
    TextView logTextView;
    Barbecue barbecue;
    Waiter waiter;
    MuttonCommand muttonCommand;
    ChickenCommand chickenCommand;
    NewCommand newCommand;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_command);
        setTitle("命令模式");

        muttonCheckBox = findViewById(R.id.checkBoxMutton);
        chickenCheckBox = findViewById(R.id.checkBoxChicken);
        newCheckBox = findViewById(R.id.checkBoxNew);
        logTextView = findViewById(R.id.textViewLog);
        barbecue = new Barbecue();
        waiter = new Waiter();
        muttonCommand = new MuttonCommand(barbecue);
        chickenCommand = new ChickenCommand(barbecue);
        newCommand = new NewCommand(barbecue);
    }

    public void onOrderClick(View view) {
        if (muttonCheckBox.isChecked()) {
            waiter.setOrder(muttonCommand);
        }
        if (chickenCheckBox.isChecked()) {
            waiter.setOrder(chickenCommand);
        }
        if (newCheckBox.isChecked()) {
            waiter.setOrder(newCommand);
        }
        logTextView.setText(waiter.show());
    }

    public void onCancelClick(View view) {
        if (muttonCheckBox.isChecked()) {
            waiter.cancelOrder(muttonCommand);
        }
        if (chickenCheckBox.isChecked()) {
            waiter.cancelOrder(chickenCommand);
        }
        if (newCheckBox.isChecked()) {
            waiter.cancelOrder(newCommand);
        }
        logTextView.setText(waiter.show());
    }

    public void onNoticeClick(View view) {
        waiter.notice();
        logTextView.setText(waiter.show());
        waiter.reset();
    }

    public void onResetClick(View view) {
        waiter.reset();
        logTextView.setText(waiter.show());
    }
}